package net.sunxiaolong.tool.captureinsureknowledge.controller;

import net.sunxiaolong.tool.captureinsureknowledge.entity.PiccKnowledge;
import net.sunxiaolong.tool.captureinsureknowledge.repository.PiccKnowledgeRepository;
import net.sunxiaolong.tool.captureinsureknowledge.tool.NetworkTool;
import net.sunxiaolong.tool.captureinsureknowledge.tool.capture.PiccCapture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author 晓龙
 * @create 2018-08-16 下午4:34
 * @desc
 **/
@RestController
@RequestMapping(value = "picc", method = RequestMethod.POST)
public class PiccCaptureController {

    @Autowired
    private PiccKnowledgeRepository piccKnowledgeRepository;

    @RequestMapping(value = "jiBenGaiNian")
    public String jiBenGaiNianCapture() throws XPatherException {
        String baseUrl = "http://www.e-picclife.com";
        String firstPageUrl = baseUrl + "/ECPL/insureAuditorium/junior/riskType/index.jsp";
        HtmlCleaner hc = new HtmlCleaner();
        String firstPageContent = NetworkTool.getStringFromUrl(firstPageUrl, "GB2312");
        TagNode firstPageTn = hc.clean(firstPageContent);

        String otherPagePPathStr = "//p[@class='iq_content_menu_B_p']";
        Object[] otherPagePObjArray = firstPageTn.evaluateXPath(otherPagePPathStr);
        for (Object otherPagePObj : otherPagePObjArray) {
            TagNode pTn = (TagNode) otherPagePObj;
            String href = baseUrl + pTn.getAttributeByName("leftlink");
            PiccCapture.captureJiBenGaiNian(href, piccKnowledgeRepository);
        }
        return "OK";
    }


    @RequestMapping(value = "baoXianChanPinZhiShi")
    public String baoXianChanPinZhiShi() throws XPatherException {
        String baseUrl = "http://www.e-picclife.com";
        String firstPageUrlStr = baseUrl + "/ECPL/insureAuditorium/intermediate/health/index.jsp";
        String firstPageContent = NetworkTool.getStringFromUrl(firstPageUrlStr, "GB2312");
        TagNode firstPageTn = new HtmlCleaner().clean(firstPageContent);
        String otherPagePPathStr = "//p[@class='iq_content_menu_B_p']";
        Object[] otherPagePObjArray = firstPageTn.evaluateXPath(otherPagePPathStr);
        for (Object otherPagePObj : otherPagePObjArray) {
            TagNode pTn = (TagNode) otherPagePObj;
            String href = baseUrl + pTn.getAttributeByName("leftlink");
            PiccCapture.captureBaoXianChanPinZhiShi(href, piccKnowledgeRepository);
        }
        return "OK";
    }


    @RequestMapping(value = "export")
    public String exportExcel() throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("PICC知识库");
        int rowIndex = 0;
        int cellIndex = 0;
        Row headRow = sheet.createRow(rowIndex++);
        headRow.createCell(cellIndex++).setCellValue("DBID");
        headRow.createCell(cellIndex++).setCellValue("标题");
        headRow.createCell(cellIndex++).setCellValue("内容");
        headRow.createCell(cellIndex++).setCellValue("链接");
        List<PiccKnowledge> piccKnowledgeList = piccKnowledgeRepository.getAllData();
        for (PiccKnowledge pk : piccKnowledgeList) {
            cellIndex = 0;
            Row thisRow = sheet.createRow(rowIndex++);
            thisRow.createCell(cellIndex++).setCellValue(pk.getId());
            thisRow.createCell(cellIndex++).setCellValue(pk.getTitle());
            thisRow.createCell(cellIndex++).setCellValue(pk.getContent());
            thisRow.createCell(cellIndex++).setCellValue(pk.getUrl());
        }
        OutputStream os = new FileOutputStream("/tmp/PICC知识库" + System.currentTimeMillis() + ".xlsx");
        workbook.write(os);
        os.close();
        workbook.close();
        return "OK";
    }
}
